var express = require('express');
var session = require('express-session');
var path = require('path');
var router = express.Router();
var sqlite3 = require('sqlite3').verbose();

/* GET users listing. */
router.get('/', function (req, res, next) {
  var db = new sqlite3.Database(path.join(__dirname, "../db/data.s3db"));
  var redata = {};
  var mode = req.query.mode;
  if (mode == '1') {
    var search = req.query.search;
    if (search == '0') {
      try {
        db.serialize(function () {
          sqlStr = "SELECT * from poundlist join material on poundlist.mid=material.mid where usual=='" + req.query.usual + "'";
          db.all(sqlStr, function (err, rows) {
            redata.count = rows.length;
            redata.code = '0';
            redata.data = rows;
            res.send(redata);
          });
        });
      } catch (ex) {
        console.log(ex);
      }
    } else {
      var plid = req.query.plid;
      var mname = req.query.mname;
      if (plid != '' && mname != '')
        sqlStr = "SELECT * from poundlist join material on poundlist.mid=material.mid where usual=='" + req.query.usual + "' and plid=='" + plid + ' and mname==' + mname + "'";
      else if (plid != '' && mname == '')
        sqlStr = "SELECT * from poundlist join material on poundlist.mid=material.mid where usual=='" + req.query.usual + "' and plid=='" + plid + "'";
      else if (plid == '' && mname != '')
        sqlStr = "SELECT * from poundlist join material on poundlist.mid=material.mid where usual=='" + req.query.usual + "' and mname=='" + mname + "'";
      else {
        sqlStr = "SELECT * from poundlist join material on poundlist.mid=material.mid where usual=='" + req.query.usual + "'";
      }
      db.all(sqlStr, function (err, rows) {
        redata.count = rows.length;
        redata.code = '0';
        redata.data = rows;
        res.send(redata);
      });
    }
  } else if (mode == '2') {
    var mid = req.query.mid;
    sqlStr = "SELECT * from cart join material on cart.mid=material.mid where cart.mid=='" + mid + "'";
    db.all(sqlStr, function (err, rows) {
      redata.count = 1;
      redata.code = '0';
      redata.data = [];
      redata.data[0] = {
        mimg: rows[0].mimg,
        mname: rows[0].mname,
        mtype: rows[0].mtype,
        weight1: '',
        weight2: '',
        weight3: ''
      }
      if (mid == '12') {
        redata.data[0].weight1 = '3.1';
        redata.data[0].weight2 = '1.8';
        redata.data[0].weight3 = '2.0';
      } else if (mid == '9') {
        redata.data[0].weight1 = '2.7';
        redata.data[0].weight2 = '1.5';
        redata.data[0].weight3 = '2.0';
      } else if (mid == '7') {
        redata.data[0].weight1 = '0.5';
        redata.data[0].weight2 = '0.2';
        redata.data[0].weight3 = '0.3';
      }
      res.send(redata);
    });
  } else if (mode == '3') {
    var mid = req.query.mid;
    sqlStr = "SELECT * from poundlist join material on poundlist.mid=material.mid where poundlist.mid=='" + mid + "'";
    db.all(sqlStr, function (err, rows) {
      redata = rows[0];
      res.send(redata);
    });
  }

});

module.exports = router;